find_package(PkgConfig)
if (PkgConfig_FOUND)
        pkg_search_module( PC_CACARD libcacard)
#        pkg_check_modules(GLIB2 IMPORTED_TARGET glib-2.0)
endif()

find_library(CACARD_LIBRARY
             NAMES cacard
             HINTS ${PC_CACARD_LIBDIR} ${CACARD_ROOT_DIR}/lib)

find_path( CACARD_INCLUDE_DIR
        NAMES
                libcacard.h
        HINTS
                "${PC_CACARD_INCLUDEDIR}"
                "${CACARD_ROOT_DIR}/include"
        PATH_SUFFIXES
                cacard
                cacard/include
        PATHS
                /opt/gnome/include
                /opt/local/include
                /sw/include
                /usr/include
                /usr/local/include
)

get_filename_component( cacardLibDir "${CACARD_LIBRARY}" PATH)

if(PC_CACARD_VERSION)
    message("PC_CACARD_VERSION is " ${PC_CACARD_VERSION})
        set(CACARD_VERSION ${PC_CACARD_VERSION})
#elseif(GLIB2_INTERNAL_INCLUDE_DIR)
#        # On systems without pkg-config (e.g. Windows), search its header
#        # (available since the initial commit of GLib).
#        file(STRINGS ${GLIB2_INTERNAL_INCLUDE_DIR}/glibconfig.h GLIB_MAJOR_VERSION
#                REGEX "#define[ ]+GLIB_MAJOR_VERSION[ ]+[0-9]+")
#        string(REGEX MATCH "[0-9]+" GLIB_MAJOR_VERSION ${GLIB_MAJOR_VERSION})
#        file(STRINGS ${GLIB2_INTERNAL_INCLUDE_DIR}/glibconfig.h GLIB_MINOR_VERSION
#                REGEX "#define[ ]+GLIB_MINOR_VERSION[ ]+[0-9]+")
#        string(REGEX MATCH "[0-9]+" GLIB_MINOR_VERSION ${GLIB_MINOR_VERSION})
#        file(STRINGS ${GLIB2_INTERNAL_INCLUDE_DIR}/glibconfig.h GLIB_MICRO_VERSION
#                REGEX "#define[ ]+GLIB_MICRO_VERSION[ ]+[0-9]+")
#        string(REGEX MATCH "[0-9]+" GLIB_MICRO_VERSION ${GLIB_MICRO_VERSION})
#        set(GLIB2_VERSION ${GLIB_MAJOR_VERSION}.${GLIB_MINOR_VERSION}.${GLIB_MICRO_VERSION})
else()
        # When using VERSION_VAR it must be set to a valid value or undefined to
        # mean "not found". It's not enough to use the empty string or any other CMake false boolean.
        unset(CACARD_VERSION)
endif()

include( FindPackageHandleStandardArgs )
find_package_handle_standard_args( MyCacard
        REQUIRED_VARS   CACARD_LIBRARY CACARD_INCLUDE_DIR
        VERSION_VAR     CACARD_VERSION
)

if( MyCacard_FOUND )
            #set ( GLIB2_DLLS
            #        "${GLIB2_DLL_DIR}/glib-2.0-0.dll"
            #)

            #set ( GLIB2_LIBS
            #        "${GLIB2_LIB_DIR}/glib-2.0.lib"
            #)

            #set ( GLIB2_PDBS
            #        "${GLIB2_DLL_DIR}/glib-2.0-0.pdb"
            #)
elseif( MyCacard_FIND_REQUIRED )
	message( SEND_ERROR "Package required but not found" )
else()
 #       set( GLIB2_DLL_DIR )
 #       set( GLIB2_PDBS )
        #set( GLIB2_DLLS )
endif()

